package com.liuli.service.impl;

 
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.MessagingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


import com.liuli.dao.BaseDao;
import com.liuli.dao.UserInfoDao;
import com.liuli.entity.UserInfo;
import com.liuli.service.UserService;
import com.liuli.utils.DateUtil;
import com.liuli.utils.EmailManager;
import com.liuli.utils.MD5Util;

@Service("us")
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfo> implements UserService {

	
	@Autowired
	@Qualifier("userdao")
	private  UserInfoDao userdao;
	
	@Override
	public UserInfo login(String username, String password) {
		//对密码进行MD5加密
		String pw = MD5Util.makeMD5(password);
		return userdao.login(username, pw);
	}

	
	public UserInfoDao getUserdao() {
		return userdao;
	}

	@Resource(name ="userdao")
	public void setUserdao(BaseDao<UserInfo> basedao) {
		super.setBasedao(basedao);
	}


	@Override
	public void register(UserInfo userInfo) {
		String pw = MD5Util.makeMD5(userInfo.getPassword());
		userInfo.setPassword(pw);
		userInfo.setRole(0); // 权限
		userInfo.setStatu("0");  //  是否激活
		userInfo.setMaxresumes(20); //投多少简历
		userInfo.setResumesnum(0); //简历
		userInfo.setAddtime(DateUtil.getNowTime());
		userdao.save(userInfo);
		
		//发邮件
		String type ="login";
		//个人用户注册才马上发邮件
		String balance =userInfo.getBalance();
		if("0".equals(balance)){
			String to = userInfo.getUsername();
			String subject="人才信息网";
			try {
				EmailManager.sendHtmlMail(type, to, subject,userInfo.getCode());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		}
		
		
		
	}


	@Override
	public UserInfo isUserExits(String username) {
		return userdao.isUserExits(username);
	}
	
	
	

}
